<?php

namespace backend\modules\config\controllers;

use backend\extensions\ApiBaseController;
use backend\services\config\AdminMenuService;

class AdminMenuController extends ApiBaseController
{

    /**
     * 菜单列表
     * * */
    public function actionList()
    {
        $params = \Yii::$app->request->get();
        $rules = [
            [['name'], 'default', 'value' => 0, 'message' => '名称检索'],
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
            [['page_size'], 'default', 'value' => 10000, 'message' => '每页显示总数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = AdminMenuService::getList($this->params);
        return $this->jsonSuccess($data);
    }

    /**
     * 添加菜单
     * * */
    public function actionSave()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['name', 'route', 'key'], 'required', 'message' => '{attribute}属必填项'],
            [['id', 'parent', 'type', 'mode'], 'default', 'value' => 0, 'message' => '设默认值'],
            [['sort', 'component', 'desc', 'icon'], 'default', 'value' => '', 'message' => '可不填参数设置'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        if ($this->params['id']) {
            return AdminMenuService::update($this->params);
        }
        unset($this->params['id']);
        return AdminMenuService::create($this->params);
    }

    /**
     * 添加菜单
     * * */
    public function actionDetail()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return AdminMenuService::detail($this->params);
    }


    /**
     * 菜单删除
     * * */
    public function actionDel()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return AdminMenuService::delete($this->params);
    }
}
